/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases.informeTecnico;

import conexionBD.ConexionServidor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import managers.managerModificarExpediente;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author olvarey
 */
public class RegistroFotografico implements Serializable {

    List<String> images;
    String rutaCompleta;
    
    private static final int BUFFER_SIZE = 6124;

    public RegistroFotografico() {
    }

    public List<String> getImages() {
        return images;
    }

    public String getRutaCompleta() {
        return rutaCompleta;
    }

    public void setRutaCompleta(String rutaCompleta) {
        this.rutaCompleta = rutaCompleta;
    }

    @PostConstruct
    public void init() {
        images = new ArrayList<String>();
        ConexionServidor miConexion = new ConexionServidor();
        try {
            Connection con = miConexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from registro_fotografico where idexpediente = ?");
            ps.setString(1, managerModificarExpediente.numeroExpediente); //se le debe mandar el numero de expediente
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                //Extraccion de informacion
                images.add(rs.getString("ubiccarpeta"));
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void uploadPhoto(FileUploadEvent e) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        String nombreArchivoCompleto = e.getFile().getFileName();
        String[] splitFullName = nombreArchivoCompleto.split("\\.");
        String rutaPadre = extContext.getRealPath("//imagenesInmuebles");
        String fileNumber = managerModificarExpediente.numeroExpediente;
        File carpeta = new File(rutaPadre + "//" + fileNumber);
        if (!carpeta.exists()) {
            carpeta.mkdir();
        }

        File result = new File(extContext.getRealPath("//imagenesInmuebles") + "//" + fileNumber + "//" + fileNumber + "-" + picturesNumber() + "." + splitFullName[1]);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;
            InputStream inputStream = e.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }

            fileOutputStream.close();
            inputStream.close();
            rutaCompleta = "..//imagenesInmuebles//" + fileNumber + "//" + fileNumber +"-"+picturesNumber()+ "." + splitFullName[1];
            FacesMessage msg = new FacesMessage("SUBIDA DE ARCHIVO", "El archivo ha sido subido con exito");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (IOException error) {
            error.printStackTrace();
            FacesMessage mensajeError = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, mensajeError);
        }
    }

    public int picturesNumber() {
        int NumeroFilas = 0;
        ConexionServidor miConexion = new ConexionServidor();
        ResultSet rs = null;
        try {
            Connection con = miConexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select count (*) from registro_fotografico where idexpediente = ?");
            ps.setString(1, managerModificarExpediente.numeroExpediente);
            rs = ps.executeQuery();
            rs.next();
            NumeroFilas = rs.getInt(1);
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return (NumeroFilas+1);
    }
}
